package com.hzdh.account.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hzdh.account.entity.goods.ToDayGoods;
import com.hzdh.account.entity.goods.ToDayPay;
import com.hzdh.account.entity.goods.ToDayPayDetails;
import com.hzdh.account.entity.login.User;
import com.hzdh.account.repository.goods.ToDayGoodsMapper;
import com.hzdh.account.repository.goods.ToDayPayDetailsMapper;
import com.hzdh.account.repository.goods.ToDayPayMapper;
import com.hzdh.account.request.goods.ToAccountDetailRequest;
import com.hzdh.account.request.goods.ToAccountHzRequest;
import com.hzdh.account.request.goods.ToGoodsRequest;
import com.hzdh.account.service.GoodsAccountService;
import com.hzdh.account.service.UserService;
import com.hzdh.account.utils.DateUtils;

@Service
@Transactional
public class GoodsAccountServiceImpl implements GoodsAccountService {

	@Autowired
	private ToDayGoodsMapper toDayGoodsMapper;
	@Autowired
	private ToDayPayDetailsMapper toDayPayDetailsMapper;
	@Autowired
	private ToDayPayMapper toDayPayMapper;
	@Autowired
	private UserService userService;
	
	@Override
	public void save(ToAccountDetailRequest accountDetailRequest, ToAccountHzRequest accountHzRequest,
			ToGoodsRequest goodsRequest) {
		
		if(accountDetailRequest == null || accountHzRequest == null || goodsRequest == null) {
			return;
		}
		
		User user = userService.querySessionUser();
		
		String dayCode = goodsRequest.getDayCode();
		String platSource = goodsRequest.getPlatSource();
		
		// 取货信息保存
		for (int i = 0; i < goodsRequest.getGoodsTypeCode().size(); i++) {
			ToDayGoods dayGoods = new ToDayGoods();
			dayGoods.setDayTime(DateUtils.strToFullDate(goodsRequest.getDayTime()));
			dayGoods.setDayCode(dayCode);
			dayGoods.setPlatSource(platSource);
			dayGoods.setGoodsSource(goodsRequest.getGoodsSource().get(i));
			dayGoods.setGoodsNumber(goodsRequest.getGoodsNumber().get(i));
			dayGoods.setGoodsColor(goodsRequest.getGoodsColor().get(i));
			dayGoods.setGoodsSize(goodsRequest.getGoodsSize().get(i));
			dayGoods.setInPrice(goodsRequest.getInPrice().get(i));
			dayGoods.setNumber(Integer.parseInt(goodsRequest.getNumber().get(i)));
			dayGoods.setSumPrice(goodsRequest.getSumPrice().get(i));
			dayGoods.setTypeCode(goodsRequest.getGoodsTypeCode().get(i));
			dayGoods.setConfirmPerson(goodsRequest.getGoodsConfirmPerson().get(i));
			dayGoods.setRemake(goodsRequest.getGoodsRemake().get(i));
			dayGoods.setCreateBy(user.getUserId());
			toDayGoodsMapper.insertSelective(dayGoods);
			
		}
		
		// 流水明细信息保存
		for (int i = 0; i < accountDetailRequest.getTypeCode().size(); i++) {
			ToDayPayDetails dayPayDetails = new ToDayPayDetails();
			dayPayDetails.setDayCode(dayCode);
			dayPayDetails.setPlatSource(platSource);
			dayPayDetails.setPayTime(DateUtils.strToFullDate(accountDetailRequest.getPayTime()));
			dayPayDetails.setTypeCode(accountDetailRequest.getTypeCode().get(i));
			dayPayDetails.setPayPrice(accountDetailRequest.getPayPrice().get(i));
			dayPayDetails.setDescribetion(accountDetailRequest.getDescribe().get(i));
			dayPayDetails.setConfirmPerson(accountDetailRequest.getDetailConfirmPerson().get(i));
			dayPayDetails.setRemake(accountDetailRequest.getDetailRemake().get(i));
			dayPayDetails.setCreateBy(user.getUserId());
			toDayPayDetailsMapper.insertSelective(dayPayDetails);
		}
		
		// 汇总信息保存
		ToDayPay toDayPay = new ToDayPay(); 
		BeanUtils.copyProperties(accountHzRequest, toDayPay);
		toDayPay.setPlatSource(platSource);
		toDayPay.setCreateBy(user.getUserId());
		ToDayPay dayPay = toDayPayMapper.selectDayPayByDaycode(dayCode);
		if (dayPay == null) {
			toDayPayMapper.insertSelective(toDayPay);
		} else {
			toDayPay.setId(dayPay.getId());
			toDayPayMapper.updateByPrimaryKeySelective(toDayPay);
		}
	}

}
